﻿using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublicClass.CadWatch
{
    /// <summary>
    /// Database 事件 – 对保存图形、添加删除修改对象、插入块参考、添加和修改外部图形（xrefs）等作出反应。
    /// </summary>
    public class CadDataBaseWatch
    {
        private Database DB = null;
        public CadDataBaseWatch(Database db = null)
        {
            DB = db;
            if (null == DB)
                DB = CadUnits.CurDb();

            InitDbWatch();
        }

        ~CadDataBaseWatch()
        {
            UnInitDbWatch();
        }

        void InitDbWatch()
        {
            if (DB == null)
                return;
            DB.ObjectAppended += ObjectEventHandler_Appended;
            DB.ObjectErased += ObjectEventHandler_Erased;

        }

        void UnInitDbWatch()
        {
            if (DB == null)
                return;
            DB.ObjectAppended -= ObjectEventHandler_Appended;
            DB.ObjectErased -= ObjectEventHandler_Erased;
        }

        private void ObjectEventHandler_Appended(object sender, ObjectEventArgs e)
        {
            CadUnits.WriteMessage("\n添加的实体:"+ e.DBObject.ToString());
        }
        private void ObjectEventHandler_Erased(object sender, ObjectErasedEventArgs e)
        {
            CadUnits.WriteMessage("\n删除的实体:"+ e.DBObject.ToString());
        }
    }
}
